<template>
	<view class="container bg-w">
		<view class="header uni-flex uni-row flex-between">
			<uni-nav-bar title="修改密码">
				<template slot='left'>
					<view class="flex width-100">
						<i class='iconfont iconfanhui' @click='goBack()'></i>
					</view>
				</template>
			</uni-nav-bar>
		</view>
		<view class="position-r-top">
			<form ref="submitForm" @submit="formSubmit" action="">
				<view class="form-list flex algin-i-c bg-w flex-between height-50 border-bottom">
					<text class="color-6 ">原密码</text>
					<input password type="text" @blur="oldPsdCheck" v-model="formData.oldPSD" placeholder="请输入原密码" onkeypress="javascript:if(event.keyCode == 32)event.returnValue = false;"	/>
				</view>
				<view class="warning-box">
					{{this.warning1}}
				</view>
				<view class="form-list flex algin-i-c bg-w flex-between height-50 border-bottom">
					<text class="color-6 ">新密码</text>
					<input password   onfcous @blur="psdCheck" type="text" v-model="userDto.newpassword1" placeholder="请输入新密码" onkeypress="javascript:if(event.keyCode == 32)event.returnValue = false;"	/>
				</view>
				<view class="warning-box">
					{{this.warning2}}
				</view>
				<view class="form-list flex algin-i-c bg-w flex-between height-50 border-bottom">
					<text class="color-6 ">确认密码</text>
					<input class=".fz-14" password @blur="psdAgainCheck" type="text" v-model="formData.newPSD"  placeholder="请确认新密码" onkeypress="javascript:if(event.keyCode == 32)event.returnValue = false;"	/>
				</view>
				<view class="warning-box">
					{{this.warning3}}
				</view>
				<view class="uni-btn-v">
					<button form-type="submit" :disabled="doSave" class="margin-b-10">保存</button>
					<button type="default" form-type="reset" @click="reset">重置</button>
				</view>
			</form>
			<view class="warning-tip p-20 flex margin-t-30">
				<icon type="warn" size="15"></icon>
				<view class="fs-12 color-6 margin-l-10">
					大写字母+小写字母+数字+特殊字符 新密码长度要不低于8位
				</view>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	import {changePassword} from '@/common/api/profile.js'
	import { login } from '@/common/api/api.js'
	export default {
		data() {
			return {
				formData:{
					oldPSD:null,
					newPSD:null,
				},
				warning1:null,
				warning2:null,
				warning3:null,
				doSave:false,
				userDto:{}
			}
		},
		created() {
			this.userDto.phone = uni.getStorageSync('user_info').phone;
			this.userDto.username = uni.getStorageSync('user_info').username;
			this.userDto.userId = uni.getStorageSync('user_info').id
		},
		methods: {
			formSubmit(){
				console.log(this.userDto);
				if(this.warning1 || this.warning2 || this.warning3){
					console.log("请看提示内容");
					
				}
				else {
					this.userDto.password = this.formData.oldPSD;
					changePassword(this.userDto).then(res => {
						if(res.code == "20000"){
							uni.showToast({
								title:"修改成功"
							});
							setTimeout(()=>{
								uni.navigateTo({
									url:'/pages/Login/Login'
								})
							},500)
						}else{
							this.reset();
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration:3000
							})
						}
					})
				}
				
			},
			oldPsdCheck(){
				this.warning1 = this.formData.oldPSD ? null : '原密码不能为空!';
				this.formData.oldPSD = this.formData.oldPSD ? this.formData.oldPSD.replace(/\s+/g,"") : null;
				let oldPassword = uni.getStorageSync('user_info').password;
				// this.warning1 = this.oldPSD ===oldPassword ? null : "* 密码错误"
			},
			psdCheck(){
				console.log(this.userDto.newpassword1);
				 let reg = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}');
				 if(this.userDto.newpassword1){
					 this.userDto.newpassword1 = this.userDto.newpassword1.replace(/\s+/g,"");
					 console.log(this.userDto.newpassword1);
					 if(this.userDto.newpassword1.length < 6) {
					 	this.warning2 = "* 密码不能少于8位"
					 	console.log(this.warning2)
					 	return;
					   } else if (this.userDto.newpassword1.length > 16) {
					 	this.warning2 = "* 密码最长不能超过16位！'));"
					 	console.log(this.warning2)
					 	return;
					   } else if(!reg.test(this.userDto.newpassword1)){
					 	this.warning2 = "* 密码格式输入有误!";
					 	console.log(this.warning2)
					 	return ;
					 }else{
					 	this.warning2 = null;
					 }
				 }
			},
			psdAgainCheck(){
				console.log(this.formData.newPSD);
				this.formData.newPSD = this.formData.newPSD ? this.formData.newPSD.replace(/\s+/g,"") : null;
					if(!this.formData.newPSD){
						this.warning3 = "* 确认密码不能为空!"
						console.log(this.warning3)
					}else if(this.userDto.newpassword1 != this.formData.newPSD){
						this.warning3 = "* 两次密码输入不一致!"
						console.log(this.warning3)
					}else{
						this.warning3 = null;
						this.doSave = false;
					}
			},
			reset(){
				this.warning1 = null;
				this.warning2 = null;
				this.warning3 = null;
				this.formData.oldPSD = null;
				this.formData.newPSD = null;
				this.formData.password = null;
				this.userDto.newpassword1 = null;
			}
			
		}
	}
</script>

<style scoped>
.form-list{padding: 0 3%;}
.uni-btn-v{position: fixed;bottom: 10px;left: 0;right: 0;padding: 0 10px;}
.uni-btn-v button{background-color: rgba(9, 170, 233, 1);border-radius: 20px; color: #fff;}
</style>
